<template>
    <div>
        <div v-for="(item,ind) in shelfList">
            <input type="checkbox" v-model="item.isCheck" @click="checkFun(ind)">
            <h2>{{item.title}}</h2>
        </div>
        <input type="checkbox" v-model="all" @click="checkAll">
        <button @click="del">删除</button>
    </div>
</template>
<script>
export default {
    created(){
        let data = JSON.parse(localStorage.getItem('addList'))||[];
        data.forEach(item => {
            item.isCheck = false;
        })
        this.shelfList = data;
    },
    data(){
        return {
            shelfList:[],
            all:false,
            choose:[]
        }
    },
    methods:{
        checkAll(){
            this.all = !this.all;
            this.shelfList.forEach(item => {
                item.isCheck = this.all;
            })
        },
        checkFun(ind){
            this.shelfList[ind].isCheck = !this.shelfList[ind].isCheck;
            this.all = this.shelfList.every(item => item.isCheck);
            if(this.shelfList[ind].isCheck){
                this.choose.push(this.shelfList[ind].id);
            }
        },
        del(){
            if(this.all){
                localStorage.clear();
                this.shelfList.splice(0);
            }else{
                let data = JSON.parse(localStorage.getItem('addList'))||[];
                data.forEach((item,index) => {
                    this.choose.forEach(v => {
                        if(item.id === v){
                            data.splice(index,1);
                        }
                    })
                })

                this.shelfList = data;

                localStorage.setItem('addList',JSON.stringify(data))
            }
        }
    }
}
</script>